package net.yangying.androidframework.core.view;

import net.yangying.androidframework.R;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

/**
 * @author YANGYING
 * @date 2011-5-25
 */
public class TitleBarLayout extends RelativeLayout {

	private static final String TAG = TitleBarLayout.class.getSimpleName();
	
    public Button leftButton;
    public RelativeLayout progressButton;
    public Button rightButton;
    public TextView tvInfo;
    public LinearLayout ll_title_searchbar;
    public Button searchButton;
    public EditText et_search;
    public ImageView iv_searchbar_delete;
    
    public static final int TITLE_HOME          =   0;
    public static final int TITLE_BACK          =   1;
    public static final int TITLE_BACK_SEARCH   =   2;
    public static final int TITLE_BACK_PHONE    =   3;
//    public static final int TITLE_HOME_PUSH     =   4;
//    public static final int TITLE_HOME_CUSTOM   =   5;

    public TitleBarLayout(Context paramContext, int layoutId) {
        super(paramContext);
        LayoutInflater localLayoutInflater = (LayoutInflater) paramContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View titlebar = localLayoutInflater.inflate(R.layout.title_bar, null);
        RelativeLayout.LayoutParams layoutparams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); // 
        layoutparams.addRule(RelativeLayout.ALIGN_PARENT_TOP);// CENTER_HORIZONTAL
        this.addView(titlebar, layoutparams);

        View centerView = localLayoutInflater.inflate(layoutId, null);
        RelativeLayout.LayoutParams localLayoutParams = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
        localLayoutParams.addRule(RelativeLayout.BELOW, R.id.mainTitleBar);
        this.addView(centerView, localLayoutParams);

        this.leftButton = (Button) findViewById(R.id.btn_titleBack);
        this.tvInfo = (TextView) findViewById(R.id.titleText);
        this.rightButton = (Button) findViewById(R.id.btn_titleMenu);
        this.progressButton = (RelativeLayout)findViewById(R.id.rlProgressBar);
        
//        ll_title_searchbar = (LinearLayout) findViewById(R.id.ll_title_searchbar);
//        et_search = (EditText) findViewById(R.id.search_aTv);
//        searchButton = (Button) findViewById(R.id.search_btn);
//        iv_searchbar_delete = (ImageView) findViewById(R.id.iv_searchbar_delete);
    }

    public void setTitle(String paramString) {
        this.tvInfo.setText(paramString);
    }

    public void setButtonTypeAndInfo(int type, String left_btntext, String title,
            String right_btntext) {
        if (!TextUtils.isEmpty(title)) {
            this.tvInfo.setText(title);
        }
        switch (type) {
        	case TITLE_HOME:
        		this.leftButton.setVisibility(INVISIBLE);
        		this.rightButton.setVisibility(INVISIBLE);
        		break;
            case TITLE_BACK:
                this.leftButton.setText(left_btntext);
                this.leftButton.setVisibility(VISIBLE);
                this.rightButton.setVisibility(INVISIBLE);
                break;
//            case TITLE_BACK_SEARCH:
//                this.leftButton.setText(left_btntext);
//                this.leftButton.setVisibility(VISIBLE);
//                this.rightButton.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.title_search));
//                this.rightButton.setVisibility(VISIBLE);
//                break;
//            case TITLE_BACK_PHONE:
//                this.leftButton.setText(left_btntext);
//                this.leftButton.setVisibility(VISIBLE);
//                this.rightButton.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.title_phone));
//                this.rightButton.setVisibility(VISIBLE);
//                break;
        }
        this.tvInfo.setVisibility(VISIBLE);
    }

    public void setTitleProgressVisibility(boolean flag)
    {
        if(flag)
            progressButton.setVisibility(View.VISIBLE);
        else
            progressButton.setVisibility(View.GONE);
    }

    public void setTitleSearchBarVisibility(boolean flag){
    	if(flag)
    		ll_title_searchbar.setVisibility(View.VISIBLE);
        else
        	ll_title_searchbar.setVisibility(View.GONE);
    }
    
    public void setTitleSearchBarEditText(String text){
    	et_search.setText(text);
    }
    public void setTitleSearchBarEditTextHint(String hint){
    	et_search.setHint(hint);	
    }
    public void setTitleSearchBarIME(int imeOptions){
    	et_search.setImeOptions(imeOptions);
    }
    public void setOnSearchBarListener(final Context context){
    	et_search.setOnEditorActionListener(new OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView arg0, int actionId, KeyEvent event) {
            	/**
            	 * 按软键盘的对应的提交按键时，触发按钮的响应
            	 */
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                	searchButton.performClick();
                }
                return true;
            }
        });
    	
    	et_search.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if(!hasFocus){
					ll_title_searchbar.setVisibility(View.GONE);
					InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);  
		            imm.hideSoftInputFromWindow(et_search.getWindowToken(), 0);
				}
			}
		});
    	
		et_search.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				if (s.length() > 0) {
					iv_searchbar_delete.setVisibility(View.VISIBLE);
					searchButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.search_btn_highlight_blue));
				} else {
					iv_searchbar_delete.setVisibility(View.GONE);
					searchButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.search_btn));
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});
		iv_searchbar_delete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				et_search.setText("");
			}
		});
    }
    
    public EditText getSearchBarEditText(){
    	return this.et_search;
    }
    
    public void updateTitleProgress(int progress)
    {
        
    }
}
